$.fn.extend({
    //---元素拖动插件
    dragging: function (data) {
        var $this = $(this);
        var xPage;
        var yPage;
        var X;//
        var Y;//
        var xRand = 0;//
        var yRand = 0;//
        var father = $this.parent();
        var defaults = {
            move: 'both',
            randomPosition: true,
            hander: 1
        }
        var opt = $.extend({}, defaults, data);
        var movePosition = opt.move;
        var random = opt.randomPosition;

        var hander = opt.hander;

        if (hander == 1) {
            hander = $this;
        } else {
            hander = $this.find(opt.hander);
        }


        //---初始化
        father.css({"position": "relative", "overflow": "hidden"});
        $this.css({"position": "absolute"});
        hander.css({"cursor": "move"});

        var faWidth = father.width();
        var faHeight = father.height();
        var thisWidth = $this.width() + parseInt($this.css('padding-left')) + parseInt($this.css('padding-right'));
        var thisHeight = $this.height() + parseInt($this.css('padding-top')) + parseInt($this.css('padding-bottom'));

        var mDown = false;//
        var positionX;
        var positionY;
        var moveX;
        var moveY;

        if (random) {
            $thisRandom();
        }
        function $thisRandom() { //随机函数
            $this.each(function (index) {
                var randY = parseInt(Math.random() * (faHeight - thisHeight));///
                var randX = parseInt(Math.random() * (faWidth - thisWidth));///
                if (movePosition.toLowerCase() == 'x') {
                    $(this).css({
                        left: randX
                    });
                }

            });
        }

        hander.mousedown(function (e) {
            father.children().css({"zIndex": "0"});
            $this.css({"zIndex": "1"});
            mDown = true;
            X = e.pageX;
            Y = e.pageY;
            positionX = father.position().left;
            positionY = father.position().top;

            return false;
        });

        $(document).mouseup(function (e) {
            mDown = false;
        });

        $(document).mousemove(function (e) {
            xPage = e.pageX;//--
            moveX = positionX + xPage - X;
            $("#position").text("moveX:" + moveX + "positionX:" + positionX + ", xPage: " + xPage + "X:" + X);

            yPage = e.pageY;//--
            moveY = positionY + yPage - Y;

            $("#xy").text("X: " + xPage + ", Y: " + yPage);

            function thisXMove() { //x轴移动
                if (mDown == true) {
                    $this.css({"left": moveX});
                } else {
                    return;
                }

                var diff = $(".takeaway_ul").width() - $("#takeaway_box").width();
                //alert(diff);//954
                if (moveX < -diff) {
                    $this.css({"left": $("#takeaway_box").width()});
                }
                if (moveX > (diff)) {
                    $this.css({"left": diff});
                }

                return moveX;
            }

            if (movePosition.toLowerCase() == "x") {
                thisXMove();
            }
        });
    }
});
